from django.db import models


'''项目信息表操作'''


class ProjectInfoManager(models.Manager):
    def insert_project(self, **kwargs):
        self.create(**kwargs)

    def update_project(self, id, **kwargs):  # 如此update_time才会自动更新！！
        obj = self.get(id=id)
        obj.project_name = kwargs.get('project_name')
        obj.simple_desc = kwargs.get('simple_desc')
        obj.save()

    def get_project_name(self, project_name):
        return self.filter(project_name__exact=project_name).count()

    def get_project_obj_by_project_name(self, project_name):
        return self.get(project_name=project_name)

    def get_project_obj_by_project_id(self, project_id):
        return self.get(id=project_id)

    def get_project_info(self):
        return self.all()

    def get_project_id_by_project_name(self, project_name):
        return self.get(project_name=project_name).id

    def get_project_name_by_project_id(self, project_id):
        return self.get(id=project_id).project_name


'''页面信息表操作'''


class PageInfoManager(models.Manager):
    def insert_page(self, **kwargs):
        self.create(**kwargs)

    def update_page(self, id, **kwargs):
        obj = self.get(id=id)
        obj.page_name = kwargs.get('page_name')
        obj.simple_desc = kwargs.get('simple_desc')
        obj.save()

    def get_page_name(self, page_name):
        return self.filter(page_name__exact=page_name).count()

    def get_page_obj_by_project_name(self, project_id):
        return self.filter(belong_project=project_id).page_name

    def get_page_obj_by_page_id(self, page_id):
        return self.get(id=page_id)

    def get_page_id_by_page_name(self, page_name):
        return self.get(page_name=page_name).id

    def get_page_obj_by_page_name(self, page_name):
        return self.get(page_name=page_name)

    def get_page_name_by_page_id(self, page_id):
        return self.get(id=page_id).page_name

    def get_page_obj_by_project_id(self, project_id):
        return self.filter(belong_project=project_id)


'''定位类型表操作'''


class LocationInfoManager(models.Manager):
    def get_location_id_by_location_type(self, location_type):
        return self.get(location_type=location_type).id

    def get_location_obj_by_location_id(self, location_id):
        return self.get(id=location_id)


'''元素信息表操作'''


class ElementInfoManager(models.Manager):
    def insert_element(self, **kwargs):
        self.create(**kwargs)

    def update_element(self, id, **kwargs):
        obj = self.get(id=id)
        obj.element_name = kwargs.get('element_name')
        obj.element_value = kwargs.get('element_value')
        obj.belong_location_id = kwargs.get('belong_location')
        obj.belong_page_id = kwargs.get('belong_page')
        obj.simple_desc = kwargs.get('simple_desc')
        obj.save()

    def get_element_name(self, element_name, type=True, id=None):
        if type:
            return self.filter(element_name__exact=element_name).count()
        else:
            if id is not None:
                return self.get(id=id).element_name
            else:
                return self.get(id=element_name)

    def get_element_obj_count(self, page_id, element_value, location_id):
        return self.filter(belong_page=page_id, element_name=element_value, belong_location=location_id).count()

    def get_element_obj_by_element_id(self, element_id):
        return self.get(id=element_id)


'''步骤信息表操作'''


class TestStepInfoManager(models.Manager):
    def insert_step(self, **kwargs):
        self.create(**kwargs)

    def update_step(self, id, **kwargs):
        obj = self.get(id=id)
        obj.element_name = kwargs.get('step_name')
        obj.belong_element = kwargs.get('belong_element')
        obj.belong_opration = kwargs.get('belong_opration')
        obj.input_data = kwargs.get('input_data')
        obj.save()

    def get_step_name(self, step_name, type=True, id=None):
        if type:
            return self.filter(step_name__exact=step_name).count()
        else:
            if id is not None:
                return self.get(id=id).step_name
            else:
                return self.get(project_name__exact=step_name)

    def get_step_obj_count(self, input_data=None, belong_element_id=None, belong_opration_id=None, step_must=None):
        return self.filter(input_data=input_data, belong_element_id=belong_element_id,
                           belong_opration_id=belong_opration_id, step_must=step_must).count()

    def get_id_obj_step(self, input_data=None, belong_element_id=None, belong_opration_id=None, step_must=None):
        query_set = self.filter(input_data=input_data, belong_element_id=belong_element_id,
                                belong_opration_id=belong_opration_id, step_must=step_must)
        print(query_set)
        if len(query_set) > 1:
            query_set[1].delete()

        return query_set[0].id


'''用例信息表操作'''


class TestCaseInfoManager(models.Manager):
    def insert_case(self, **kwargs):
        self.create(**kwargs)

    def update_case(self, id, **kwargs):
        obj = self.get(id=id)
        obj.case_name = kwargs.get('case_name')
        obj.belong_project_id = kwargs.get('belong_project_id')
        obj.steps = kwargs.get('steps')
        obj.hooks = kwargs.get('hooks')
        obj.simple_desc = kwargs.get('simple_desc')
        obj.save()

    def get_case_obj_by_id(self, case_id):
        return self.get(id=case_id)

    def get_case_obj_count(self, case_name):
        return self.filter(case_name=case_name).count()


'''环境信息管理'''


class TestSuiteInfoManager(models.Manager):
    def insert_suite(self, **kwargs):
        self.create(**kwargs)

    def update_suite(self, id, **kwargs):
        obj = self.get(id=id)
        obj.suite_name = kwargs.get('suite_name')
        obj.belong_project = kwargs.get('belong_project')
        obj.cases = kwargs.get('cases')
        obj.status = 'False'
        obj.save()

    def get_suite_obj(self):
        self.all()

    def get_suite_obj_count(self, suite_name):
        return self.filter(suite_name=suite_name).count()


'''环境信息管理'''


class EnvInfoManager(models.Manager):
    def insert_env(self, **kwargs):
        self.create(**kwargs)

    def update_env(self, index, **kwargs):
        obj = self.get(id=index)
        obj.env_name = kwargs.pop('env_name')
        obj.base_url = kwargs.pop('base_url')
        obj.simple_desc = kwargs.pop('simple_desc')
        obj.capabilities = kwargs.pop('capabilities')
        obj.rule = kwargs.pop('rule')
        obj.save()

    def get_env_name(self, index):
        return self.get(id=index).env_name

    def delete_env(self, index):
        self.get(id=index).delete()

    def get_env_obj_count(self, env_name):
        return self.filter(env_name=env_name).count()


'''resource包管理'''


class ResourceInfoManager(models.Manager):
    def insert_resource(self, **kwargs):
        self.create(**kwargs)

    def update_resource(self, index, **kwargs):
        obj = self.get(id=index)
        obj.simple_desc = kwargs.pop('simple_desc')
        obj.save()

    def get_resource_obj_count(self, resource_name):
        return self.filter(resource_name=resource_name).count()


'''操作信息管理'''


class OprationInfoManager(models.Manager):
    def get_opration_info(self):
        return self.all()

    def get_id_by_opration_name(self, opration_name):
        return self.get(opration_name=opration_name).id


'''测试报告信息管理'''


class ReportInfoManager(models.Manager):
    def insert_report(self, **kwargs):
        self.create(**kwargs)


'''运行日志信息管理'''


class LogInfoManager(models.Manager):
    def insert_log(self, **kwargs):
        self.create(**kwargs)

    def get_log_name_by_id(self, log_id):
        return self.get(id=log_id).log_name


class DebugTalkManager(models.Manager):
    def insert_debugtalk(self, **kwargs):
        self.create(**kwargs)

    def update_debugtalk(self, **kwargs):
        obj = self.get(id=1)
        obj.debugtalk = kwargs.pop('debugtalk')
        obj.save()


class SystemSettingsManager(models.Manager):
    def insert_system(self, **kwargs):
        kwargs['id'] = 1
        self.create(**kwargs)

    def update_system(self, **kwargs):
        obj = self.get(id=1)
        obj.main_dir = kwargs.pop('main_dir')

        server = kwargs.pop('server', None)
        if server:
            obj.server = server
        else:
            obj.server = '127.0.0.1'

        obj.log_dir = kwargs.pop('log_dir')
        obj.image_dir = kwargs.pop('image_dir')
        obj.resource_dir = kwargs.pop('resource_dir')

        rabbitMq_url = kwargs.pop('rabbitMq_url', None)
        if rabbitMq_url:
            obj.rabbitMq_url = rabbitMq_url
        else:
            obj.rabbitMq_url = 'amqp://guest:guest@127.0.0.1:5672//'

        obj.email_user = kwargs.pop('email_user')
        obj.email_password = kwargs.pop('email_password')
        obj.save()

